import { reactive, watch, onMounted, onUnmounted } from 'vue';
import { queryPageListHis } from '@api/url/equipmentManagement';
import router from '@/router';

export default function importConfig() {
  const base = reactive<any>({
    pagination: {
      total: 0, //总条数
      current: 1,
      pageSize: 10,
    },
    tabelList: null,
    checkFrom: {
      department: null,
      brevityCode: null,
      dispatchNumber: null,
      materialChargeNumber: null,
      operatorCode: null,
      operatorName: null,
      process: null,
      productName: null,
      productNo: null,
      supplierName: null,
      workOrder: null,
    },
    rowSelection: {
      type: 'checkbox',
      showCheckedAll: true,
      onlyCurrent: false,
    },
    expandable: {
      title: '详情',
      width: 80,
      expandedRowRender: (record): any => {
        return record.tc001 + record.tc002;
      },
    },
    config: {
      header: ['设备名称', '计数产量', '米数', '状态'],
      data: [],
      align: ['center'],
    },
    intervalId: null,
  });

  /**
   * @exports getTable 销售订单列表数据获取
   */
  const getTable = () => {
    const obj = {
      size: base.pagination.pageSize,
      current: base.pagination.current,
    };

    queryPageListHis(obj).then((res: any) => {
      if (res.code == '200') {
        base.tabelList = res.data.list;
        base.tabelList.map((rev: any, index: any) => {
          rev.key = index;
        });
        base.pagination.total = res.data.total;
      }
    });
  };

  const goBack = () => {
    router.go(-1);
  };

  watch(
    () => base.pagination,
    () => {
      getTable();
    },
    {
      deep: true,
    },
  );

  onMounted(() => {
    getTable();

    // 创建定时器，每5秒执行一次
    base.intervalId = setInterval(function () {
      getTable();
    }, 5000);
  });

  onUnmounted(() => {
    clearInterval(base.intervalId);
  });

  return {
    base,
    goBack,
  };
}
